package com.meituan.catering.management.product.biz.service.impl;

import com.meituan.catering.management.common.model.biz.UserContextBO;
import com.meituan.catering.management.product.api.http.model.request.CreateProductHttpRequest;
import com.meituan.catering.management.product.api.http.model.request.SaveProductHttpRequest;
import com.meituan.catering.management.product.api.http.model.request.UpdateProductHttpRequest;
import com.meituan.catering.management.product.biz.model.ProductBO;
import com.meituan.catering.management.product.biz.model.converter.ProductBOConverter;
import com.meituan.catering.management.product.biz.service.ProductMethodBizService;
import com.meituan.catering.management.product.dao.mapper.ProductMethodMapper;
import com.meituan.catering.management.product.dao.model.ProductMethodDO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 做法组 接口实现
 */
@Service
public class ProductMethodBizServiceImpl implements ProductMethodBizService {
    @Resource
    ProductMethodMapper productMethodMapper;

    /**
     * 根据商品号查询出 方法组集合
     * @param tenantId
     * @param productId
     * @return
     */
   public  List<ProductBO.MethodGroup> findAllByProductId(Long tenantId, Long productId){
       List<ProductMethodDO> allByProductId = productMethodMapper.findAllByProductId(tenantId, productId);
       List<ProductBO.MethodGroup> methodGroups=new ArrayList<>();
       if (allByProductId.size()!=0){
           methodGroups = this.groupMeth(allByProductId);
       }
       return methodGroups;
   }

    /**
     * 批量插入 方法组
     * @param userContextBO
     * @param methodGReq
     * @param productId
     * @return
     */
    @Override
    public int insetList(UserContextBO userContextBO, List<CreateProductHttpRequest.MethodGroup> methodGReq, Long productId) {
        List<ProductMethodDO> methodDOS = this.buildProductMethodDO(userContextBO, methodGReq, productId);
        /**
         * 批量插入
         */
        int insert=productMethodMapper.insertList(methodDOS);
        return insert;
    }

    @Override
    public Integer delete(UserContextBO userContextBO, Long productId) {
        return productMethodMapper.deleteByProductId(userContextBO.getTenantId(),productId);
    }

    @Override
    public List<Long> updataList(UserContextBO userContextBO, List<UpdateProductHttpRequest.MethodGroup> methodGroups, Long productId) {
        List<ProductMethodDO> methodDOS = this.buildProductMethodDOUpd(userContextBO, methodGroups, productId);
        List<Long> ids=new ArrayList<>();
        for (ProductMethodDO metDO:methodDOS) {
            int insert = productMethodMapper.insert(metDO);
            ids.add(metDO.getId());
        }
        return ids;
    }

    /**
     * 构建方法组 DO
     * @param userContextBO
     * @param requests
     * @param productId
     * @return
     */

    private List<ProductMethodDO> buildProductMethodDOUpd(UserContextBO userContextBO, List<UpdateProductHttpRequest.MethodGroup> requests, Long productId)
    {
        List<ProductMethodDO> methodDOS=new ArrayList<>();

        /**
         * 存入Map
         */
        for (SaveProductHttpRequest.MethodGroup meth:requests) {

            for (SaveProductHttpRequest.MethodGroup.Option accOption: meth.getOptions()) {
                ProductMethodDO methodDO=new ProductMethodDO();
                methodDO.setGroupName(meth.getName());
                methodDO.setTenantId(userContextBO.getTenantId());
                methodDO.setProductId(productId);
                methodDO.setName(accOption.getName());
                methodDOS.add(methodDO);
            }
        }
        return methodDOS;
    }




    /**
     * 构建方法组 DO
     * @param userContextBO
     * @param requests
     * @param productId
     * @return
     */

    private List<ProductMethodDO> buildProductMethodDO(UserContextBO userContextBO, List<CreateProductHttpRequest.MethodGroup> requests, Long productId)
    {
        List<ProductMethodDO> methodDOS=new ArrayList<>();

        /**
         * 存入Map
         */
        for (SaveProductHttpRequest.MethodGroup meth:requests) {

            for (SaveProductHttpRequest.MethodGroup.Option accOption: meth.getOptions()) {
                ProductMethodDO methodDO=new ProductMethodDO();
                methodDO.setGroupName(meth.getName());
                methodDO.setTenantId(userContextBO.getTenantId());
                methodDO.setProductId(productId);
                methodDO.setName(accOption.getName());
                methodDOS.add(methodDO);
            }
        }
        return methodDOS;
    }


    /**
     * 方法组集合 分组 加上转BO
     */
    private  List<ProductBO.MethodGroup> groupMeth(List<ProductMethodDO> productMethodDOS){
        Map<String, List<ProductMethodDO>> collect = productMethodDOS.stream().collect(Collectors.groupingBy(ProductMethodDO::getGroupName));
        List<ProductBO.MethodGroup> methodGroups=new ArrayList<>();
        collect.forEach((k,v)->{
            ProductBO.MethodGroup methodGroup=new ProductBO.MethodGroup();
            methodGroup.setName(k);
            methodGroup.getOptions().addAll(v.stream().map(ProductBOConverter::fromDOToMethOption).collect(Collectors.toList()));
            methodGroups.add(methodGroup);
        });
        return methodGroups;

    }


}
